import { Component, ViewChild, ChangeDetectorRef, Output, EventEmitter } from "@angular/core";
import { TableComponent, PaginationPositionDisplayEnum } from "../../../../shared/components/table/table.component";
import { ColumnOptions } from "../../../../shared/components/table/models/columnOptions.model";
import { AppComponent } from "../../../../app.component";
import { TableOptions } from "../../../../shared/components/table/models/tableOptions.model";
import { ResearchSustainableProductsService } from "../researchSustainableProducts.service";
import { TableRouterLink } from "../../../../shared/components/table/models/tableRouterLink.model";
import { BaseComponent } from "../../../../shared/components/baseComponent/base.component";


@Component({
    selector: 'sustainableProducts-list',
    templateUrl: './sustainableProductsCompanyList.component.html',
    styleUrls: ['./sustainableProductsCompanyList.component.scss', '../../../../../assets/table.scss'],
    providers: [ResearchSustainableProductsService]
})

export class SustainableProductsCompanyList extends BaseComponent{
    public paginationPosition = PaginationPositionDisplayEnum.UpAndDown;
    public columns: ColumnOptions[];
    public source: any[];
    public elementCount: number;
    public sPIndicators: any;
    public sPIndicatorsCategories: any[];
    public filter: any = {};
    @ViewChild('table') table: TableComponent;

    @Output() changed: EventEmitter<TableOptions> = new EventEmitter();
    
    constructor(
        private cdRef: ChangeDetectorRef,        
        private rootComponent: AppComponent,
        private sustainableProductsService: ResearchSustainableProductsService
    ) {
        super(cdRef);
    }

    ngOnInit() {

        this.source = [];
        this.sPIndicatorsCategories =[];

        if(!this.productPermissions.sustainableproducts) 
        {
            this.rootComponent.finishLoading();
            return;
        }
        this.getBulkCompanies(new TableOptions("CompanyName"));            
        this.setColumns();
    }


    private setColumns() {
        this.columns = [
            new ColumnOptions('CompanyName', 'companyName', 'Company Name', 'col-md-2', true, false, false, undefined, false, true, false),
            new ColumnOptions('exchange', 'exchange', 'Exchange', 'col-md-1', false, false, false, undefined, false, false, false, true, undefined, false, false, true),
            new ColumnOptions('ticker', 'ticker', 'Ticker', 'col-md-1', false, false, false, undefined, false, false, false, true, undefined, false, false, true),
            new ColumnOptions('ariaOfInvolvement', 'ariaOfInvolvement', 'Area of Involvement', 'col-md-4', false, false, false, undefined, false, false, false, true, undefined, false, false, true, true),
            new ColumnOptions('levelOfInvolvement', 'levelOfInvolvement', 'Level of Involvement', 'col-md-2', false, false, false, undefined, false, false, false, true, undefined, false, false, true, true),
            new ColumnOptions('industry', 'industry', 'Subindustry', 'col-md-2', false, false, false, undefined, false, false, false, true, undefined, false, false, true)
        ];
    }

    public getBulkCompanies(tableOptions: TableOptions): void {
        if (tableOptions.pageOptions.listOnly == false) {
            this.sustainableProductsService.getCompaniesCount(tableOptions.filter).subscribe(data => {
                this.elementCount = data;
            });
        }

        this.sustainableProductsService.getCompanies(tableOptions.filter, tableOptions.pageOptions.skip, tableOptions.pageOptions.take, tableOptions.orderByCol, tableOptions.orderByAsc).subscribe(data => {
            
            if (tableOptions.pageOptions.listOnly == false) {
                this.table.ResetPaginationToFirstPage();
            }
            let valuesToDisplay: any[];
            valuesToDisplay = data.map(company => {

                let areaOfInvolvement = [];
                let levelOfInvolvement = [];

                company.indicators.forEach(ind => {
                    areaOfInvolvement.push( { bold: true, name: ind.name});
                    levelOfInvolvement.push('');

                    ind.categories.forEach(category => {
                        areaOfInvolvement.push( {indent: true, name: category.name} );
                        levelOfInvolvement.push(category.levelOfInvolvement)
                    });
                });

                return {
                    companyName: new TableRouterLink(company.name, true, company.companyNameSafeUrl, 'sustainableProducts'),
                    exchange: company.exchange,
                    ticker: company.ticker,
                    areaOfInvolvement: areaOfInvolvement,
                    levelOfInvolvement: levelOfInvolvement,
                    industry: company.industry
                };
            });
            this.source = valuesToDisplay;

        });

    }

    public tableOptionsChanged(tableOptions: TableOptions): void {
        this.changed.emit(tableOptions);
        //this.getBulkCompanies(.updateTableOptions(this.landingFilter, tableOptions));
    }

    ngAfterViewInit() {
        this.rootComponent.finishLoading();
    }

}

